using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SideShow.Scf.ObjectModel;
using SideShowClient;

namespace PocketPCSideShowClient
{
    public partial class ScfMenuControl : ScfUserControl
    {
        private bool _initializing;

        public ScfMenuControl(ScfNavigator navigator)
            : base(navigator)
        {
            InitializeComponent();
        }

        protected override void BindData()
        {
            _initializing = true;
            lvItems.Items.Clear();
            imlSmallImages.Images.Clear();
            ListViewItem def = null;
            ScfMenu scfMenu = (ScfMenu)this.ScfObject;
            foreach (ScfObject obj in scfMenu.Children)
            {
                if (obj is ScfMenuItem)
                {
                    ScfMenuItem item = (ScfMenuItem)obj;
                    int i = -1;
                    if (item.ImageId != 0)
                    {
                        Image img = Navigator.GetImage(item.ImageId);
                        Util.MakeTransparent((Bitmap)img, lvItems.BackColor);
                        imlSmallImages.Images.Add(img);
                        i = imlSmallImages.Images.Count - 1;                
                    }
                    if (item.Enabled)
                    {
                        ListViewItem lvi = new ListViewItem(item.Text);
                        lvi.Tag = item;
                        lvi.ImageIndex = i;
                        lvItems.Items.Add(lvi);
                        if (item.IsDefault)
                        {
                            def = lvi;
                        }
                    }
                }
            }
            if (def == null && lvItems.Items.Count > 0)
            {
                def = lvItems.Items[0];
            }
            if (def != null)
            {  
                def.Selected = true;
            }
            _initializing = false;
        }

        private void lvMenuItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvItems.SelectedIndices.Count > 0)
            {
                ScfMenuItem item = (ScfMenuItem)lvItems.Items[lvItems.SelectedIndices[0]].Tag;
                if (item.ContextMenuId != 0)
                {
                    ScfContextMenu ctxMenu = this.GetContextMenu(item.ContextMenuId, item);
                    lvItems.ContextMenu = ctxMenu.ContextMenu;
                }
            }
        }

        private void ScfMenuControl_Resize(object sender, EventArgs e)
        {
            columnHeader1.Width = lvItems.ClientSize.Width;
        }

        private void lvItems_ItemActivate(object sender, EventArgs e)
        {
            if (!_initializing && lvItems.SelectedIndices.Count > 0)
            {
                ScfMenuItem item = (ScfMenuItem)lvItems.Items[lvItems.SelectedIndices[0]].Tag;
                Navigator.Navigate(item);
            }
        }
    }
}
